package topoSort;

import java.awt.Color;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.Vector;

public class Vertex extends Ellipse2D.Double implements Selectable
{
	public static final int VERTEX_WIDTH = 20;
	public static final int VERTEX_HEIGHT = 20;
	public static final double BOLD_SCALE = 1.5;
	private static final long serialVersionUID = 1532460984257499270L;
	private Vector<Edge> m_links = new Vector<Edge>();
	private int m_weight = 1;
	private Color m_color = Color.BLACK;
	private Color m_selectionColor = Color.GRAY;
	boolean m_selected = false;

	public Vertex(double x, double y)
	{
		super(x, y, VERTEX_WIDTH, VERTEX_HEIGHT);
	}

	public void setCoords(double x, double y)
	{
		this.x = x;
		this.y = y;
		for (Edge e : m_links)
		{
			e.refreshPos();
		}
	}

	public Color getColor()
	{
		if (!m_selected)
		{
			return m_color;
		}
		else
		{
			return m_selectionColor;
		}
	}

	public void setColor(Color c)
	{
		m_color = c;
		m_selectionColor = m_color.darker();
	}

	public Vector<Edge> getLinks()
	{
		return m_links;
	}

	public void addLink(Edge link)
	{
		m_links.add(link);
	}

	@Override
	public int hashCode()
	{
		final int prime = 31;
		int result = super.hashCode();
		result = prime * result + ((m_color == null) ? 0 : m_color.hashCode());
		result = prime * result + (m_selected ? 1231 : 1237);
		result = prime
				* result
				+ ((m_selectionColor == null) ? 0 : m_selectionColor.hashCode());
		result = prime * result + m_weight;
		return result;
	}

	@Override
	public boolean equals(Object obj)
	{
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		Vertex other = (Vertex) obj;
		if (m_color == null)
		{
			if (other.m_color != null)
				return false;
		}
		else if (!m_color.equals(other.m_color))
			return false;
		if (m_selected != other.m_selected)
			return false;
		if (m_selectionColor == null)
		{
			if (other.m_selectionColor != null)
				return false;
		}
		else if (!m_selectionColor.equals(other.m_selectionColor))
			return false;
		if (m_weight != other.m_weight)
			return false;
		return true;
	}

	public void removeLink(Edge link)
	{
		m_links.remove(link);
	}

	public int getWeight()
	{
		return m_weight;
	}

	public void setBold(boolean b)
	{
		if (b)
		{
			this.setFrame(new Rectangle2D.Double(this.x, this.y, VERTEX_WIDTH
					* BOLD_SCALE, VERTEX_HEIGHT * BOLD_SCALE));
		}
		else
		{
			this.setFrame(new Rectangle2D.Double(this.x, this.y, VERTEX_WIDTH,
					VERTEX_HEIGHT));
		}
	}

	public void setWeight(int w)
	{
		m_weight = w;
	}

	@Override
	public void setFrame(Rectangle2D r)
	{
		// Refresh position of every linked edge
		super.setFrame(r);
		for (Edge e : m_links)
		{
			e.refreshPos();
		}
	}

	public void setSelected(boolean b)
	{
		m_selected = b;
	}
}
